<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{
              item.pubdate | fromNow
            }}</span>
            <van-button size="mini" type="default" @click="showReply(item)"
              >回复({{ item.reply_count }})</van-button
            >
          </p>
        </div>
        <van-icon
          slot="right-icon"
          @click="likeComments(item)"
          :name="item.is_liking ? 'like' : 'like-o'"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button slot="button" size="mini" type="info" @click="publish"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- 回复评论 -->
    <van-action-sheet v-model="show" title="评论回复">
      <Reply
        v-if="show"
        :currentComment="currentComment"
        @addReplyCount="addReplyCountFn"
        :artId="artId"
      ></Reply>
    </van-action-sheet>
  </div>
</template>

<script>
import {
  getComment,
  toComment,
  likeComment,
  delLikeComment
} from '@/api/article'
import Reply from './reply.vue'
export default {
  name: 'ArticleComment',
  components: {
    Reply
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      show: false,
      currentComment: {}
    }
  },
  computed: {
    artId() {
      return this.$route.params.id
    }
  },
  methods: {
    async onLoad() {
      const res = await getComment(this.$route.params.id, this.offset)
      console.log('获取评论', res)
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      this.offset = res.data.last_id
      if (res.data.results.length === 0) {
        this.finished = true
      }
    },
    async publish() {
      if (!this.content) {
        return this.$toast('请输入内容')
      }
      const res = await toComment(this.artId, this.content)
      console.log('发布评论', res)
      this.list.unshift(res.data.new_obj)
      this.$toast('发布成功')
      this.content = ''
    },
    async likeComments(item) {
      // 判读是否点赞
      if (item.is_liking) {
        await delLikeComment(item.com_id)
      } else {
        await likeComment(item.com_id)
      }
      //   console.log('评论点赞', res)
      item.is_liking = !item.is_liking
    },
    showReply(item) {
      this.show = true
      this.currentComment = item
    },
    addReplyCountFn() {
      this.currentComment.reply_count++
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
